package aj.controller;

import aj.bo.AjActivationCode;
import aj.bo.AjJs;
import aj.service.AjActivationCodeService;
import framework.util.fileUpload.bo.SysFileModule;
import framework.common.Common;
import framework.common.ResponseVo;
import framework.util.fileUpload.service.SysFileModuleService;
import framework.util.fileUpload.service.SysFileService;
import framework.util._BeanUtil;
import framework.util.jdkExt._StringUtil;
import framework.util.layui._LayuiUtil;
import framework.util.queryFilter.service.impl.QueryFilter;
import framework.util.queryFilter.vo.OrderBy;
import framework.util.queryFilter.vo.Where;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import frameworkExt.util.controller.BaseControllerExt;
import aj.service.AjJsService;;import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/aj/AjJs.do")
public class AjJsController extends BaseControllerExt {

    @Resource
    private AjJsService service;
    @Resource
    private SysFileModuleService sysFileModuleService;
    @Resource
    private SysFileService sysFileService;
    @Resource
    private AjActivationCodeService ajActivationCodeService;

    @ResponseBody
    @RequestMapping(params = "method=listByJmLid")
    public ResponseVo listByJmLid(HttpServletRequest request, HttpServletResponse response){
        String jmLid = request.getParameter("jmLid");
        if(StringUtils.isBlank(jmLid)){
            return sendSuccess();
        }

        QueryFilter filter = new QueryFilter(request, false);
        filter.where(AjJs.JM_LID, Where.eq, jmLid);
        filter.where(AjJs.IS_DELETE, Where.eq, Common.IS_NO);
        List<AjJs> list = service.getAll(filter);
        for (AjJs ajJs : list) {
            sysFileService.getFiles(ajJs, "jsFile");
        }

        return sendSuccess(list);
    }

    @RequestMapping(params = "method=toAdd")
    public ModelAndView toAdd(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
        String pageUrl = request.getParameter("pageUrl");
        if(StringUtils.isAnyBlank(pageUrl)){
            return null;
        }
        SysFileModule jsFile = sysFileModuleService.getByTableNameAndModuleEname("aj_js", "jsFile");

        model.addObject("sysFileModule_jsFile", jsFile.getLid());
        model.addObject("uuid", UUID.randomUUID().toString());
        model.setViewName(pageUrl);
        return model;
    }

    @ResponseBody
    @RequestMapping(params = "method=add")
    public ResponseVo add(AjJs bo, HttpServletRequest request, HttpServletResponse response){
        //验证版本号是否唯一，而且是否比之前版本号都大
        QueryFilter maxFilter = new QueryFilter(_LayuiUtil.FIRST_PAGE, 1);
        //maxFilter.where(AjJs.IS_DELETE, Where.eq, Common.IS_NO); //也要比删除了的大，因为删除的版本可能存在用户
        maxFilter.where(AjJs.JM_LID, Where.eq, bo.getJmLid());
        maxFilter.orderBy(AjJs.VERSION, OrderBy.DESC);
        AjJs oldLastestJs = service.get(maxFilter);
        if(null != oldLastestJs && !_StringUtil.isGreatThan(bo.getVersion(), oldLastestJs.getVersion())){
            return sendFail("目前最新的版本号为: " + oldLastestJs.getVersion() + "！新增的版本号必须要比目前最新的版本号大", ResponseVo.LAYER_TYPE_OPEN);
        }

        //如果有旧的版本，则要给新版本生成激活码。以让旧用户可以免费升级到新版本的时候，可以正常使用
        if(null != oldLastestJs){
            QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
            filter.where(AjActivationCode.JS_LID, Where.eq, oldLastestJs.getLid());
            List<AjActivationCode> oldCodeList = ajActivationCodeService.getAll(filter);
            service.add(bo, oldCodeList);
        }else{
            service.add(bo);
        }
        return sendSuccess();
    }

    @RequestMapping(params = "method=toEditOrDetail")
    public ModelAndView toEditOrDetail(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
        String lid = request.getParameter("lid");
        String pageUrl = request.getParameter("pageUrl");
        if(StringUtils.isAnyBlank(lid, pageUrl)){
            return null;
        }

        AjJs bo = service.get(Integer.valueOf(lid));
        sysFileService.getFiles(bo, "jsFile");

        SysFileModule jsFile = sysFileModuleService.getByTableNameAndModuleEname("aj_js", "jsFile");

        model.addObject("sysFileModule_jsFile", jsFile.getLid());
        model.addObject("bo", bo);
        model.setViewName(pageUrl);
        return model;
    }

    @ResponseBody
    @RequestMapping(params = "method=update")
    public ResponseVo update(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        String lid = request.getParameter("lid");
        if(StringUtils.isBlank(lid)){
            return sendFail("参数错误");
        }

        try {
            AjJs oldBo = service.get(Integer.valueOf(lid));
            _BeanUtil.copyPropertiesFromRequest(oldBo, request);
            service.update(oldBo);
            return sendSuccess();
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(params = "method=delete")
    public ResponseVo delete(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
        String lids = request.getParameter("lids");
        if (StringUtils.isBlank(lids)) {
            return sendFail("参数错误");
        }

        List<AjJs> list = new ArrayList<>();
        for(String lid : lids.split(",")){
            AjJs bo = service.get(Integer.valueOf(lid));
            list.add(bo);
        }
        service.removeFakeBatch(list);

        return sendSuccess();
    }

}
